home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0008_MATHSPD.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  1KB  |  39 lines

  1. {
  2. > I was just wondering how to speed up some math-intensive
  3. > routines I've got here. For example, I've got a Function
  4. > that returns the distance between two Objects:
  5.  
  6. > Function Dist(X1,Y1,X2,Y2 : Integer) : Real;
  7. > begin
  8. >   Dist := Round(Sqrt(Sqr(X1-X2)+Sqr(Y1-Y2)));
  9. > end;
  10.  
  11. > This is way to slow. I know assembly can speed it up, but
  12. > I know nothing about as. so theres the problem. Please
  13. > help me out, any and all source/suggestions welcome!
  14.  
  15. X1, Y1, X2, Y2 are all Integers.  Integer math is faster than Real (just
  16. about anything is).  Sqr and Sqrt are not Integer Functions.  Try for
  17. fun...
  18. }
  19.  
  20. Function Dist( X1, Y1, X2, Y2 : Integer) : Real;
  21. Var
  22.   XTemp,
  23.   YTemp : Integer;
  24. { the allocation of these takes time.  if you don't want that time taken,
  25.   make them global With care}
  26. begin
  27.   XTemp := X1 - X2;
  28.   YTemp := Y1 - Y2;
  29.   Dist  := Sqrt(XTemp * XTemp + YTemp * YTemp);
  30. end;
  31.  
  32. {
  33. if you have a math coprocessor or a 486dx, try using DOUBLE instead of
  34. Real, and make sure your compiler is set to compile For 287 (or 387).
  35. }
  36.  
  37. begin
  38.   Writeln('Distance Between (3,9) and (-2,-3) is: ', Dist(3,9,-2,-3) : 6 : 2);
  39. end.